// src/middleware/response.middleware.js
import { logger } from './logger.middleware.js';

// 统一的响应中间件
const sendResponse = (status, code, message, data = null, error = null) => {
    return {
        status,
        code,
        message,
        data,
        error
    };
};

// 响应成功的方法
const sendSuccess = (res, code, message, data) => {
    logger.info(`Response: ${code} ${message}`, { data });
    res.status(code).json(sendResponse(true, code, message, data));
};

// 响应失败的方法
const sendError = (res, code, message, error = null) => {
    logger.error(`Response: ${code} ${message}`, { error });
    res.status(code).json(sendResponse(false, code, message, null, error));
};

// 统一返回结构的中间件
const responseMiddleware = (req, res, next) => {
    res.sendSuccess = sendSuccess.bind(null, res);
    res.sendError = sendError.bind(null, res);
    next();
};

export default responseMiddleware;